package com.ruoyi.dataAnalysis.service.impl;

import com.ruoyi.dataAnalysis.domain.DataResponse;
import com.ruoyi.dataAnalysis.domain.DeviceIdTagTime;
import com.ruoyi.dataAnalysis.domain.PhotoTimeAndTagManual;
import com.ruoyi.dataAnalysis.mapper.DataAnalysisMapper;
import com.ruoyi.dataAnalysis.service.DataAnalysisService;
import com.ruoyi.traplight.service.TrapLightDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author Mr-Le
 */
@Service
public class DataAnalysisServiceImpl implements DataAnalysisService {

    @Autowired
    private DataAnalysisMapper dataAnalysisMapper;

    @Autowired
    private TrapLightDeviceService deviceService;

    /**
     * 查找 昆虫总数 列表
     * 按时间查找
     * @param dataResponse 参数，暂未用到
     * @return 列表
     */
    @Override
    public List<String> selectInsectTotalList(DataResponse dataResponse) {
        String area = dataResponse.getArea();
        List<String> insectList = new ArrayList<>();
        List<Integer> deviceIdList = deviceService.selectDeviceIdByarea(area);
        for(int id :deviceIdList){
            dataResponse.setDeviceId(id);
            insectList.addAll(dataAnalysisMapper.selectInsectTotalList(dataResponse));
        }
        return insectList;
    }
    @Override
    public List<String> selectInsectTotalListByDevcieId(DataResponse dataResponse,String deviceIds) {
        List<String> insectList = new ArrayList<>();
        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));
        for(String id :deviceIdList){
            dataResponse.setDeviceId(Integer.valueOf(id));
            insectList.addAll(dataAnalysisMapper.selectInsectTotalList(dataResponse));
        }
        return insectList;
    }
    @Override
    public List<String> selectInsectTotalListByDeviceIds(DataResponse dataResponse,String deviceIds) {
        List<String> insectList = new ArrayList<>();
        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));
        List<DeviceIdTagTime> deviceIdTagTimeList = dataAnalysisMapper.selectInsectListByPeriod(dataResponse);
        for(DeviceIdTagTime deviceIdTagTime:deviceIdTagTimeList){
            for(String id :deviceIdList){
                if(deviceIdTagTime.getDeviceId() == (Integer.valueOf(id))){
                    insectList.add(deviceIdTagTime.getTagManual());
                }
            }
        }
        return insectList;
    }
    /**
     *  按天查找虫子列表
     * @param dataResponse 日期和地区
     * @return 列表
     */
    @Override
    public List<String> selectInsectByDay(DataResponse dataResponse) {
        String area = dataResponse.getArea();
        List<String> insectList = new ArrayList<>();
        List<Integer> deviceIdList = deviceService.selectDeviceIdByarea(area);
        for(int deviceId : deviceIdList){
            dataResponse.setDeviceId(deviceId);
            insectList.addAll(dataAnalysisMapper.selectInsectByDay(dataResponse));
        }
        return insectList;
    }

    @Override
    public List<String> selectInsectByDeviceId(String deviceIds,DataResponse dataResponse){
        List<String> insectList = new ArrayList<>();
        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));
        for(String id :deviceIdList){
            dataResponse.setDeviceId(Integer.valueOf(id));
            insectList.addAll(dataAnalysisMapper.selectInsectByDay(dataResponse));
        }
        return insectList;
    }

    public List<PhotoTimeAndTagManual> selectInsectByDeviceIds(String deviceIds, DataResponse dataResponse){
        List<PhotoTimeAndTagManual> insectList = new ArrayList<>();
        List<DeviceIdTagTime> deviceIdTagTimeLst = dataAnalysisMapper.selectInsectByPeriod(dataResponse);
        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));
        for(DeviceIdTagTime deviceIdTagTime : deviceIdTagTimeLst){
            for(String id :deviceIdList){
                if(deviceIdTagTime.getDeviceId() == (Integer.valueOf(id))){
                    PhotoTimeAndTagManual photoTimeAndTagManual = new PhotoTimeAndTagManual();
                    photoTimeAndTagManual.setPhotoTime(deviceIdTagTime.getPhotoTime())
                            .setTagManual(deviceIdTagTime.getTagManual());
                    insectList.add(photoTimeAndTagManual);
                }
            }
        }
        return insectList;
    }

}
